package com.monochromebytes.mononet.packet;

import java.net.InetSocketAddress;

/**
 * An incoming UDP packet.
 * 
 * It has an timestamp storing the timestamp of its arrival. The timestamp can
 * be set on arrival by setArrivedNow().
 * 
 * @author ted
 */
public class IncomingPacket extends Packet {

    /**
     * The timestamp of the arrival of this packet.
     * 
     * It is 0 if the packet is not set as arrived, yet.
     */
    private long timestamp;

    /**
     * Creates a new IncomingPacket with the given length, its sender and
     * recipient.
     * 
     * @param length
     *            The length of the packet to allocate its byte array.
     * @param sender
     *            The sender of this Packet.
     * @param recipient
     *            The recipient of this Packet.
     */
    public IncomingPacket(int length, InetSocketAddress sender, InetSocketAddress recipient) {
        super(length, sender, recipient);
    }

    /**
     * Mark this packet as now arrived.
     * 
     * The current timestamp will be stored as the time of arrival.
     */
    public void setArrivedNow() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * Checks if the packet is marked as arrived.
     * 
     * @return True if the packet is marked as arrived.
     */
    public boolean isArrived() {
        return this.timestamp > 0;
    }

    /**
     * Returns the timestamp of the Packets arrival.
     * 
     * @return The timestamp of the Packets arrival.
     */
    public long getTimestampOfArrival() {
        return this.timestamp;
    }
}
